package com.omg.api.handle;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.omg.api.argument.ActivityArgument;
import com.omg.api.argument.Argument;
import com.omg.api.result.Result;
import com.omg.api.result.body.ActivityBody;
import com.omg.api.result.header.ActivityHeader;
import com.omg.constant.Constants;
import com.omg.constant.LPUtility;
import com.omg.constant.ResponseCode;
import com.omg.persistence.dao.lifepoint.entity.LPActivity;
import com.omg.persistence.dao.lifepoint.entity.LPUser;
import com.omg.persistence.delegate.lifepoint.LPActivityDelegate;
import com.omg.persistence.delegate.lifepoint.LPCommentDelegate;
import com.omg.persistence.delegate.lifepoint.LPUserDelegate;

/**
 * 
 * @author xiazhiquan
 * 
 *         May 29, 2011
 */
public class ActivityHandler extends Handler {

	private static final Logger LOG = Logger.getLogger(ActivityHandler.class
			.getName());

	private static ActivityHandler instance = null;

	static {
		instance = new ActivityHandler();
	}

	public static ActivityHandler getInstance() {
		return instance;
	}

	public String handle(Argument argument) throws Exception {

		String userid = ((ActivityArgument) argument).getUserid();
		String content = ((ActivityArgument) argument).getContent();
		String action = ((ActivityArgument) argument).getAction();
		String activityid = ((ActivityArgument) argument).getActivityid();

		LPActivity activity = null;

		if (action.equals(Constants.ACTION_POST)) {
			activity = new LPActivity();
			activity.setActivityid(LPUtility.getActivityId());
			activity.setContent(content);
			activity.setCreatetime(System.currentTimeMillis());
			activity.setOwner(userid);
			activity.setUpdatetime(System.currentTimeMillis());

			int ret = LPActivityDelegate.getInstance().addLPActivity(activity);

			if (ret != 0) {
				throw new Exception(""
						+ ResponseCode.AP_E_Info_PostActivityFailed);
			}

		} else if (action.equals(Constants.ACTION_DELETE)) {
			activity = LPActivityDelegate.getInstance()
					.getActivityByActivityId(activityid);

			if (activity == null) {
				throw new Exception(""
						+ ResponseCode.AP_E_Info_InvalidActivityId);
			}

			int ret = LPActivityDelegate.getInstance()
					.delLPActivity(activityid);

			if (ret != 0) {
				throw new Exception(""
						+ ResponseCode.AP_E_Info_DeleteActivityFailed);
			}

		} else if (action.equals(Constants.ACTION_EDIT)) {
			activity = LPActivityDelegate.getInstance()
					.getActivityByActivityId(activityid);

			if (activity == null) {
				throw new Exception(""
						+ ResponseCode.AP_E_Info_InvalidActivityId);
			}

			activity.setActivityid(activityid);
			activity.setContent(content);
			activity.setUpdatetime(System.currentTimeMillis());

			int ret = LPActivityDelegate.getInstance().updateLPActivity(
					activity);

			if (ret != 0) {
				throw new Exception(""
						+ ResponseCode.AP_E_Info_UpdateActivityFailed);
			}
		}

		LPUser owner_info = LPUserDelegate.getInstance().getLPUserByUserid(
				userid);
		owner_info.setPasswd(null);
		owner_info.setSid(null);
		owner_info.setVersion(null);
		activity.setOwner(null);
		activity.setOwner_info(owner_info);
		activity.setComment_total(LPCommentDelegate.getInstance()
				.getCommentTotalByActivityId(activityid));
		return getJson(activity);
	}

	private final String getJson(LPActivity activity) throws Exception {
		try {

			// construct result
			Result result = new Result();

			// header
			ActivityHeader header = new ActivityHeader();
			header.setRespcode(ResponseCode.AP_E_Succ);
			result.setHeader(header);

			// body
			ActivityBody body = new ActivityBody();
			body.setActivity(activity);
			result.setBody(body);

			return new Gson().toJson(result);
		} catch (Exception e) {
			e.printStackTrace();
			String errorMsg = e.getMessage();
			LOG.error(errorMsg, e);
			throw new Exception(""
					+ (errorMsg != null && !errorMsg.isEmpty() ? errorMsg
							: ResponseCode.AP_E_Info_UnkownError));
		}
	}

}
